package com.blogSystem.article.python;


import org.python.core.PyObject;
import org.python.google.common.annotations.VisibleForTesting;
import org.python.modules._systemrestart;
import org.python.util.InteractiveInterpreter;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;


public class RunPython {


    public static String url="F:/blog_system/blog_system_article/src/main/java/com/blogSystem/article/predict.py";
    public void py(){


        PythonInterpreter interpreter=new InteractiveInterpreter();
        interpreter.exec("import sys");
        String path1="F:\\python\\Lib\\site-packages\\jieba";
        interpreter.exec("sys.path.append('" +path1+ "')");
        interpreter.exec("import jieba");
      /**  interpreter.exec("import jieba");
        interpreter.exec("import pickle ");
        interpreter.exec("from sklearn.naive_bayes import MultinomialNB");
        interpreter.exec("from sklearn.feature_extraction.text import TfidfTransformer");
        interpreter.exec("from sklearn.feature_extraction.text import TfidfVectorizer ");
        interpreter.exec("from sklearn.datasets.base import Bunch");**/
        //PyObject x = interpreter.get("x");
        //System.out.println(x);
    }



    public static String executepy(String args)throws IOException, InterruptedException{

        System.out.println(args);
        try {
            //定义process对象  Define process object
            Process proc = Runtime.getRuntime().exec(args);
            //获取输出流  Get the output stream
            InputStreamReader isr = new InputStreamReader(proc.getInputStream(), "GBK");
            BufferedReader in = new BufferedReader(isr);
            //定义变量准备接收返回结果 Define variables ready to receive return results
            String line = null;
            System.out.println(in);
            line=in.readLine();
            return line;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }











   public String getType(String content) throws IOException, InterruptedException {
       try {
           /**
            * 定义脚本输入源数组:python3源,python脚本路径,python需要的变量(若干)
            *
            * Define the script input source array:
            * python3 source, python script path, python required variables (several)
            *
            */
           System.out.println(url);

           String args ="F:\\python\\python.exe "+url+" "+content;
           String[] args1 = new String[] { "python", url, content};
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String date1 = df.format(System.currentTimeMillis());
           System.out.println(date1);

           /**
            * 把数组传给那个函数即可
            * Pass the array to that function.
            */

           String type=executepy(args);

            return type;

       } catch (IOException | InterruptedException e) {
           e.printStackTrace();
       }
       return "";
   }

}
